package com.example.review;

import androidx.annotation.NonNull;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.review.base.BaseActivity;
import com.example.review.controller.ActivityController;

public class chapterTwo2Activity extends BaseActivity {

    private static final String TAG = "chapterTwo2Activity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_two2);
        Log.i(TAG, "onCreate: ");
        Intent intent=getIntent();
        String  name =intent.getStringExtra("name");
        int age =intent.getIntExtra("age",0);
        Log.d(TAG, "name: "+name+" age: "+age);
        Toast.makeText(this, "name: "+name+" age: "+age, Toast.LENGTH_SHORT).show();


        /**
         * 向上个活动返回数据
         */
        Button button=findViewById(R.id.buttonN);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 =new Intent();
                intent1.putExtra("return","返回数据");
                //这个方法是专门用于向上一个活动返回数据的
                //第一个参数用于向上一个活动返回处理结果，一般只使用RESULT_OK 或RESULT_CANCELED
                // 第二个参数则把带有数据的Intent传递回去
                //setResult(1,intent1);
                setResult(RESULT_OK,intent1);
                finish();
            }
        });
        /**
         * 启动普通活动
         */
        Button buttonF =findViewById(R.id.buttonF);
        buttonF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentF =new Intent(chapterTwo2Activity.this,chapterTwo3NormalActivity.class);
                startActivity(intentF);
            }
        });


        /**
         * 启动dialog活动
         */
        Button buttonS =findViewById(R.id.buttonS);
        buttonS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentS =new Intent(chapterTwo2Activity.this,chapterTwo4DialogActivity.class);
                startActivity(intentS);
            }
        });


        /**
         * 取出活动被回收时保存的数据
         */
        if (savedInstanceState != null){
            String names=savedInstanceState.getString("name","默认名字");
            int ages =savedInstanceState.getInt("age",3);
            //Log.i(TAG, "取出来的临时保存数据： "+names+"年龄是"+ages);
            Toast.makeText(this,"取出来的临时保存数据： "+names+"年龄是"+ages,Toast.LENGTH_SHORT).show();
        }

        Button buttonText= findViewById(R.id.buttonText);
        buttonText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentv =getIntent();
                String param1 = intentv.getStringExtra("param1");
                String param2 = intentv.getStringExtra("param2");
                Toast.makeText(chapterTwo2Activity.this,"shujushi: "+param1+param2,Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 重写活动生命周期的七个方法
     */
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart: ");
    }

    /**
     * 防止活动被回收，临时保存数据
     *该方法在活动回收前一定会被调用
     */
    @Override
    //一下是保存数据,对应取出数据在oncreate方法里面
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("name","老王");
        outState.putInt("age",22);
    }


    /**
     * 再被启动的方法里面定义一个方法，指定要什么参数，让启动的活动直接调用该方法即可
     * @param context
     * @param data1
     * @param data2
     */
    public static void actionStart(Context context,String data1,String data2){
        Intent intent=new Intent(context,chapterTwo2Activity.class);
        intent.putExtra("param1",data1);
        intent.putExtra("param2",data2);
        context.startActivity(intent);
    }
}